﻿
@{
    Layout = null;
    ViewBag.Title = "Index2";
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Simple Task System</title>

    <link href="~/Content/bootstrap.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-2.1.4.js"></script>
    <script src="~/Scripts/bootstrap.js"></script>
    <script src="~/Scripts/angular.js"></script>
    <script src="~/Scripts/jquery.signalR-2.2.1.js"></script>
    <script src="~/signalr/hubs"></script>
</head>
<body>
    <h2>Index2</h2>
    <div ng-app="chatApp">

    </div>
    <script>
        var chatHub = $.connection.chatHub;
        var chatApp = angular.module('chatApp', ['ngRoute', 'ngResource', 'ngSanitize', 'ngMessages', 'ngSVGAttributes']).value('chatHub', chatHub);

        chatHub.client.broadcastTodo = function (userIds, obj) {
            //通知下级控制器有待办事项
            $scope.$broadcast('todoschanged', obj);
            //显示桌面通知
            if (obj.type == 'new') {
                //桌面通知标题
                var title = '来自[' + obj.data.ApplicantName + ']的申请单';
                //申请单类型名称
                var formTypeName = DefaultService.getEnumText(17, obj.data.Type);
                var msg = '[' + formTypeName + ']' + obj.data.Name;
                //桌面通知方法
                NotifyService.Notify('todos', title, msg);
            }
        }
        //接收推送的待办事项
        $scope.$on('todoschanged', function (d, obj) {
            $scope.$apply(function () {
                //如果是新增数据，在当前列表中添加一条
                if (obj.type == 'new') {
                    $scope.todoApps.unshift(obj.data);
                }
                else if (obj.type == 'delete') {//如果是撤销申请，则把当前列表中那条数据删除
                    for (var j = 0; j < $scope.todoApps.length; j++) {
                        if ($scope.todoApps[j].Id == obj.data.Id) {
                            $scope.todoApps.splice(j, 1);
                            break;
                        }
                    }
                }
            });
        });

        //桌面通知服务
        chatApp.factory('NotifyService', function () {

            return {
                Notify: function (icon, title, msg) {
                    // At first, let's check if we have permission for notification
                    // If not, let's ask for it
                    if (window.Notification && Notification.permission !== "granted") {
                        Notification.requestPermission(function (status) {
                            if (Notification.permission !== status) {
                                Notification.permission = status;
                            }
                        });
                    }
                    var iconPath = '/Content/images/icons/' + (icon || 'info') + '.png';
                    var options = {
                        lang: 'zh-CN',
                        body: msg,
                        icon: iconPath
                    };
                    var notify;
                    // If the user agreed to get notified
                    if (window.Notification && Notification.permission === "granted") {
                        notify = new Notification(title, options);
                    }
                    else if (window.Notification && Notification.permission !== "denied") {
                        Notification.requestPermission(function (status) {
                            if (Notification.permission !== status) {
                                Notification.permission = status;
                            }
                            if (status === "granted") {
                                notify = new Notification(title, options);
                            }
                            else {
                                console.log('您禁止了桌面通知，无法推送到您的桌面！');
                            }
                        });
                    }
                    else {
                        console.log('您禁止了桌面通知，无法推送到您的桌面！');
                    }
                    if (notify) {
                        notify.onclose = function (evt) {

                        };
                        //点击切换到浏览器
                        notify.onclick = function () {
                            window.focus();
                        };
                    }
                }
            };
        });
    </script>
</body>
</html>